<#include "../base.ftl"/>

<#macro title>
    Bean代码生成器
</#macro>

<#macro cssfile>
<link
    href="libs/dp.SyntaxHighlighter/Styles/SyntaxHighlighter.css" rel="stylesheet">
</#macro>    

<#macro scriptfile>
<!-- 代码显示用 -->
<script type="text/javascript" src="libs/dp.SyntaxHighlighter/Scripts/shCore.js"></script>  
<script type="text/javascript" src="libs/dp.SyntaxHighlighter/Scripts/shBrushJScript.js"></script>
<script type="text/javascript" src="libs/dp.SyntaxHighlighter/Scripts/shBrushJava.js"></script>
<script type="text/javascript" src="libs/dp.SyntaxHighlighter/Scripts/shBrushSql.js"></script>
<script type="text/javascript" src="libs/dp.SyntaxHighlighter/Scripts/shBrushXml.js"></script>

</#macro>

<#macro breadcrumb>
    <ul class="breadcrumb">
     <li><a href="index.html">Home</a> <span class="divider">/</span></li>
     <li class="active">Bean生成</li>
    </ul>
</#macro>

<#macro main>
    <div class="row-fluid">
        <form>
            <label>请选择数据库</label>
            <select id="dbId" name="dbId"></select>
            <p>
               <button id="tableparser" class="btn btn-primary" type="button">获取表</button>
            </p>
        </form>
        
        <div class="page-header">
                <h3>数据表</h3>
        </div>
        <table id="gridTable"></table>
        <div id="gridPager"></div>
        <p>
           <button id="codeGen" class="btn btn-primary" type="button">生成代码</button>
        </p>
        
        <div class="page-header">
            <h3>Yaml Code</h3>
        </div>
        <div class="dp-highlighter" id="yamlCode">
        </div>
        <div class="page-header">
            <h3>Java Code</h3>
        </div>
        <div class="dp-highlighter" id="javaCode">
        </div>
        
    </div>
</#macro>

<#macro jsscript>
//加载数据库记录
 $("#tableparser").click(
                    function() {
                        var url = "codeAssistant!getAllTable.action";
                        var params = {
                            "dbId" : $("#dbId").val()
                        };
                        $.post(url, params, function(data, status) {
                            fillData(data);
                            reloadGrid();
                        }, "json");
                    });
                    
//生成代码
 $("#codeGen").click(
                    function() {
                        if($("#gridTable").getGridParam("selrow") == null){
                            showMsg("请选择一张表！","error");
                            return;
                        }
                    
                        var url = "codeAssistant!genBeanCode.action";
                        var rowData = getGridData();
                        var params = {
                            "dbId" : rowData.dbId,
                            "tableName" : rowData.TABLE_NAME
                        };
                        $.post(url, params, function(data, status) {
                           $("#yamlCode").html('<pre name="code" class="sql">' + data.yamlCode + '</pre>');
                           $("#javaCode").html('<pre name="code" class="java">' + data.beanCode + '</pre>');
                           //$("#javaCode").text(data.beanCode);
                           dp.SyntaxHighlighter.ClipboardSwf = 'libs/dp.SyntaxHighlighter/Scripts/clipboard.swf';  
                           dp.SyntaxHighlighter.HighlightAll('code');  
                        }, "json");
                    });  
                    
                    
    function getGridData(){
        //拿到选中行数据
        var rowData = $("#gridTable").jqGrid("getRowData", $("#gridTable").getGridParam("selrow"));
        return rowData;
    }                 
                    
                    
    function reloadGrid() {
        $("#gridTable").setGridParam({
            'data' : tableData
        }).trigger("reloadGrid");
    }         
    
    function fillData(data) {
            tableData = [];
            $.each(data, function(i) {
                tableData.push({
                    id : data[i].TABLE_NAME,
                    TABLE_NAME : data[i].TABLE_NAME,
                    REMARKS : data[i].REMARKS,
                    dbId : data[i].dbId
                });
            });
        }           
    
    var tableData = [];

 jQuery(document).ready(function() {
        //获取数据库信息
        $.post("list/codeassistant/dbInfo/slist.html", "", function(data, status) {
               //删除所有的数据
               var maxIndex=$("#dbId option:last").attr("index");
               for(var i=0;i<maxIndex;i++){
                    $("#dbId option:last").remove();
               }
               
               //添加数据
               $.each(data.result, function(i) {
                 $("#dbId").append("<option value='" + data.result[i].DB_ID + "'>" + data.result[i].DB_NAME + "</option>");
               });
               
            }, "json");
            
        $("#gridTable").jqGrid({
            data : tableData,
            datatype : "local",
            height : 250,
            rowNum : 20,
            colNames : [ 'id','表名', '备注', 'dbId' ],
            colModel : [ {
                name : 'id',
                index : 'id',
                hidden : true
            }, {
                name : 'TABLE_NAME',
                index : 'TABLE_NAME'
            }, {
                name : 'REMARKS',
                index : 'REMARKS'
            }, {
                name : 'dbId',
                index : 'dbId',
                hidden : true
            } ],
            pager : "#gridPager",
            caption : "数据表列表",
            viewrecords : true,
            gridview : true,
            autowidth : true
        });
 });
</#macro>